import os
import re
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn
from sklearn.model_selection import train_test_split
from sklearn import metrics
import skimage
import skimage.color
import skimage.io
import skimage.feature
import skimage.transform
from sklearn.base import BaseEstimator, TransformerMixin
from skimage.color import rgb2gray
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV
import sklearn.metrics
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from mathe import rgb2gray_transform, hogtransformer

# load the data
data = pickle.load(open('data_animals_head_20.pickle','rb'))
print("Load pickle data")

dataDesc = data['description']  # There are 20 classes and 2057 images are there. All the images are 80 x 80 (rgb)
X = data['data']                # Contains all images, 
y = data['target']              # Ground truth
labels = data['labels']         # Label names of all dataset

# split the data into train and test
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,stratify=y)
print("x_train.shape: {}; x_test.shape: {}; len(y_train): {}; len(y_test): {}".format(x_train.shape,x_test.shape,len(y_train),len(y_test)))


print("Modelling pipeline.")
model_pipeline = Pipeline([
    ('grascale',rgb2gray_transform()),
    ('hogtransform',hogtransformer(orientations=8,pixels_per_cell=(10,10),cells_per_block=(3,3))),
    ('scale',StandardScaler()),
    ('sgd',SGDClassifier(loss='hinge',learning_rate='adaptive',eta0=0.001))
])

model_pipeline.fit(x_train,y_train)
y_pred = model_pipeline.predict(x_test)

# Model Evaluation
cr = sklearn.metrics.classification_report(y_test,y_pred,output_dict=True)
print(pd.DataFrame(cr).T)
print("Model evaluation score: ", metrics.cohen_kappa_score(y_test,y_pred))
